package com.turnengine.client.local.unit.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.unit.bean.IUnit;
import com.turnengine.client.local.unit.bean.UnitSerializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The Get Units By Group Id Data Serializer.
 */
public class GetUnitsByGroupIdDataSerializer extends AbstractCommandDataSerializer<List<IUnit>> implements IGetUnitsByGroupIdDataSerializer {

	/**
	 * Creates a new Get Units By Group Id Data Serializer.
	 */
	public GetUnitsByGroupIdDataSerializer() {
		super(GetUnitsByGroupId.COMMAND_ID);
	}

	@Override
	public List<IUnit> readReturnValue(IDataReader reader) throws IOException {
		List<IUnit> value = reader.readObjectCollection(new ArrayList<IUnit>(), true, false, new UnitSerializer());
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, List<IUnit> value) throws IOException {
		writer.writeObjectCollection(value, true, false, new UnitSerializer());
	}

	@Override
	public IExecutableCommand<List<IUnit>> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int id = reader.readInt();

		// Create object
		GetUnitsByGroupId object = new GetUnitsByGroupId();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setId(id);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<List<IUnit>> argument) throws IOException {
		GetUnitsByGroupId object = (GetUnitsByGroupId) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int id = object.getId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(id);
	}

	@Override
	public IExecutableCommandResponse<List<IUnit>> newResponse() {
		return new GetUnitsByGroupIdResponse();
	}
}
